#! /bin/bash


function configureDocker()
{
# 更换国内镜像源
cat >> /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://vg0bksne.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
}

function installDocker()
{
 #1.在frames.txt中查看是否需要安装flink
 dockerInfo=`egrep "^docker" ../frames.txt`

 docker=`echo $dockerInfo | cut -d " " -f1`
 isInstall=`echo $dockerInfo | cut -d " " -f2`
 dockerNodes=`echo $dockerInfo | cut -d " " -f3` 
 node=`hostname`

 #是否安装
 if [[ $isInstall = "true" && $dockerNodes =~ $node ]];then
    
     #2.查看/opt/frames目录下是否有docker安装脚本
     dockerIsExists=`find /opt/frames -name "get-docker.sh"`

     if [[ ${#dockerIsExists} -ne 0 ]];then

          chmod +x /opt/frames/get-docker.sh
          
          # 使用 Aliyun 进行加速
          sh /opt/frames/get-docker.sh --mirror Aliyun
          systemctl start docker
          systemctl enable docker
     else
         echo "/opt/frames目录下没有get-docker安装脚本"
         echo "开始下载get-docker.sh脚本"
         curl -fsSL https://get.docker.com -o /opt/frames/get-docker.sh
         chmod +x /opt/frames/get-docker.sh
         echo "下载成功，开始安装"
         sh /opt/frames/get-docker.sh --mirror Aliyun
         systemctl start docker
         systemctl enable docker
     fi
     configureDocker
 else
     echo "Docker不允许安装在当前节点，请检查配置文件！"
 fi

}

installDocker
